****************************************************************************************************************************

Date: 14.maj 2014
By: Søren Albeck Nielsen, fbni

Purpose: Create master data set for NICU project.

Latest update: 06. August 2014
******************************************************************************************************************************;
*Creates Libraries;
Libname s 'K:\Data\Workdata\702727\FBNI2727\NICU';
Libname a 'F:\Rawdata\702727\data201301';
libname c 'K:\Data\Workdata\702727\ADHD_grunddata\Datasæt';
libname d 'F:\Rawdata\702727';
libname f 'F:\rawdata\702727';
libname g '\\srvfsenas3\\formater';
libname k 'F:\rawdata\702727\data201204';
libname l 'F:\rawdata\702727\data201208';
libname h 'F:\rawdata\702727\data200809';
libname b 'K:\Data\Workdata\702727\FBNI2727\NICU\daycare'; 


*********************************************************************************
*Create variable for birth order - Nummererer moderens børn
*********************************************************************************;
data barn1;
set a.fodsel_10;
fodt_aar=year(FODTDATO);
birthyear=year(FODTDATO);
run;
Proc sort data=barn1;
by pnrm fodtdato;
run;

Data barn2;
set barn1;
d=1;
by pnrm;
if first.pnrm then birth_order=d;
else birth_order+d;
run;

Data barn3;
set barn2;
if pnrm="" then birth_order=.;
drop d;
run;


******************************************************************************************
*Create variable for the year where the mothers first child where born, birthyear_fb
******************************************************************************************;
Data barn4;
set barn3;
if birth_order=1;
birthyear_fb=year(fodtdato);
keep pnrm birthyear_fb;
run;

Data barn5;
merge barn3 (in=a) barn4;
by pnrm;
if a;
run; /*4.037.777 obs*/

Proc sort data=barn5;
by pnr;
run;

*********************************************************************************
*Check if there are duplicates in births (pnr) and remove all delete all observations with that pnr.
*********************************************************************************;
data barn6;
set barn5;
d=1;
by pnr;
if first.pnr then d1=d;
else d1+d; *Er pnr flere gange vil vi have d1+d. dvs d1=2;
run;

proc sort data=barn6;
by pnr descending d1;
run;

data barn7;
set barn6;
by pnr;
if first.pnr then d2=d1;
else d2+d1; *For alle pnr numre der går igen, vil d2 være forskellig fra 1;
run;

Data test1; *Tester hvor mange obs. det drejer sig om;
set barn7;
if d2^=1 then output test1;
run;
Data test2; *Tester hvor mange forskellige pnr det drejer sig om;
set test1;
by pnr;
if first.pnr then output test2;
run;
*Dvs. Det drejer sig om 6867 obs. og 2896 forskellige pnr-numre.
De pnr-numre som går igen slettes, da vi ikke ved hvilke forældre oplysninger der er korrekte.;

Data barn8;
set barn7;
if d2^=1 then delete;
drop d d1 d2;
run;

*******************************************************************************************
Create variable for multiple birth, multiple. - Antal søskende der har samme mor, men kan have forskellig far (uden brug af DST "antal" indikator!)
*******************************************************************************************;
proc sort data=barn8;
by pnrm fodtdato;
run;

Data temp1;
set barn8;
mult=0;
if fodtdato=lag1(fodtdato) then mult=1;
d=0;
if pnrm=lag1(pnrm) then d=1;
temp_mult=mult*d;
run;

Proc sort data=temp1;
by pnrm fodtdato descending temp_mult;
run;

Data temp2;
set temp1;
d1=lag1(temp_mult);
d2=temp_mult+d1;
if missing(d2) then d2=0;
rename d2=temp_multiple;
run;

data temp3;
set temp2;
multiple=0;
if temp_multiple>0 then multiple=1;
run;

Data barn6;
set temp3;
drop mult d temp_mult d1 temp_multiple antal;
run;

*********************************************************************************
*Create variable for family size, family_size
*********************************************************************************;
proc sort data=barn6;
by pnrm descending birth_order;
run;

data barn7;
set barn6;
by pnrm;
if first.pnrm then temp_antal_born=birth_order;
run;

Data barn8;
set barn7;
by pnrm;
if first.pnrm then family_size = temp_antal_born;
else family_size + temp_antal_born;
drop temp_antal_born;
run;


***********************************************************************************************************************
*Create variable for half siblings, half_sib. - Lig 1 hvis mor har et/flere børn med anden mand end barents far
***********************************************************************************************************************;
proc sort data=barn8;
by pnrm fodtdato;
run;

data barn9;
set barn8;
by pnrm;
if first.pnrm then temp_first_far=pnrf;
run;

data barn10;
set  barn9;
temp1_first_far=input(temp_first_far,12.);
pnrf1=input(pnrf,12.);
run;

data barn11;
set barn10;
by pnrm;
if first.pnrm then first_far=temp1_first_far;
else first_far + temp1_first_far;
drop temp_first_far temp1_first_far; 
run;

data barn12;
set  barn11;
dummy=1;
if first_far^=pnrf1 then dummy=0;
if pnrf1 =. then dummy =.;
if first_far =. then dummy=.;
run;

Data barn13;
set barn12;
keep pnrm dummy;
run;
proc sort data=barn13;
by pnrm dummy;
run;

data barn14;
set barn13;
by pnrm;
if first.pnrm then d=1;
run;

Data barn15;
set barn14;
if d=1;
run;

Data barn16;
set barn15;
if pnrm ^= "";
if dummy =0 then half_sib =1;
if dummy =1 then half_sib =0;
if dummy =. then half_sib =.;
drop d dummy;
run;

Data barn;
merge barn12 (in=a) barn16;
by pnrm;
if a;
drop dummy first_far pnrf1;
run;

*******************************************************************************************
*Create variable for BW, APGAR score and GA
*******************************************************************************************;
Data temp1;
set barn;
apgar=v_apgar;
if v_apgar =. then apgar=v_apgar5;
drop v_apgar v_apgar5;
BW = v_vagt;
if BW in(9999 9990 9920 9900) then BW=.;
drop v_vagt;
V_GA_UGER_DECIMAL = V_GA_DAGE/7;
Drop V_GA_DAGE;
V_GA_UGER = round(V_GA_UGER_DECIMAL);
Drop V_GA_UGER_DECIMAL;
GA = V_GA_UGER; if V_GA_UGER =. then GA = V_svlangde;
if GA = in(97,98,99,100) then GA=.;; 
drop V_SVLANGDE V_GA_UGER; 
run;

data temp1;
set temp1;
if apgar gt 10 then apgar =.;
run;


*******************************************************************************************
Add parents education level at birth
*******************************************************************************************;
*Henter koden fra tidligere projekt, Pollution og tilføjer 1980-2010;
data udda1980_0; set f.udda1980;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data audd ; set g.uddan_2011_audd (keep = audd afsp1e apubl1 h1 m1);
audd_num=input(audd,8.); 						*Character to numeric;
drop audd;
proc sort nodupkey dupout=dupkeys; 				*Currently no duplicates;
by audd_num;
run;
data udda1980_1; merge udda1980_0 (in = in_udda1980 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1980=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1980; merge udda1980_1 (in = in_udda1980) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1980=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1980;
proc sort;
by pnr;
run;

data udda1981_0; set f.udda1981;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1981_1; merge udda1981_0 (in = in_udda1981 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1981=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1981; merge udda1981_1 (in = in_udda1981) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1981=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1981;
proc sort;
by pnr;
run;

data udda1982_0; set f.udda1982;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1982_1; merge udda1982_0 (in = in_udda1982 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1982=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1982; merge udda1982_1 (in = in_udda1982) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1982=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1982;
proc sort;
by pnr;
run;

data udda1983_0; set f.udda1983;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1983_1; merge udda1983_0 (in = in_udda1983 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1983=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1983; merge udda1983_1 (in = in_udda1983) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1983=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1983;
proc sort;
by pnr;
run;

data udda1984_0; set f.udda1984;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1984_1; merge udda1984_0 (in = in_udda1984 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1984=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1984; merge udda1984_1 (in = in_udda1984) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1984=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1984;
proc sort;
by pnr;
run;

data udda1985_0; set f.udda1985;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1985_1; merge udda1985_0 (in = in_udda1985 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1985=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1985; merge udda1985_1 (in = in_udda1985) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1985=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1985;
proc sort;
by pnr;
run;

data udda1986_0; set f.udda1986;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1986_1; merge udda1986_0 (in = in_udda1986 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1986=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1986; merge udda1986_1 (in = in_udda1986) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1986=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1986;
proc sort;
by pnr;
run;

data udda1987_0; set f.udda1987;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1987_1; merge udda1987_0 (in = in_udda1987 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1987=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1987; merge udda1987_1 (in = in_udda1987) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1987=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1987;
proc sort;
by pnr;
run;

data udda1988_0; set f.udda1988;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1988_1; merge udda1988_0 (in = in_udda1988 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1988=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1988; merge udda1988_1 (in = in_udda1988) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1988=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1988;
proc sort;
by pnr;
run;

data udda1989_0; set f.udda1989;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1989_1; merge udda1989_0 (in = in_udda1989 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1989=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1989; merge udda1989_1 (in = in_udda1989) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1989=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1989;
proc sort;
by pnr;
run;

data udda1990_0; set f.udda1990;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
*data audd ; *set g.uddan_2011_audd (keep = audd afsp1e apubl1 h1 m1);
*audd_num=input(audd,8.); 						*Character to numeric;
*drop audd;
*proc sort nodupkey dupout=dupkeys; 				*Currently no duplicates;
*by audd_num;
*run;
data udda1990_1; merge udda1990_0 (in = in_udda1990 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1990=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1990; merge udda1990_1 (in = in_udda1990) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1990=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1990;
proc sort;
by pnr;
run;

data udda1991_0; set f.udda1991;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1991_1; merge udda1991_0 (in = in_udda1991 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1991=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1991; merge udda1991_1 (in = in_udda1991) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1991=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1991;
proc sort;
by pnr;
run;

data udda1992_0; set f.udda1992;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1992_1; merge udda1992_0 (in = in_udda1992 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1992=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1992; merge udda1992_1 (in = in_udda1992) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1992=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1992;
proc sort;
by pnr;
run;

data udda1993_0; set f.udda1993;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1993_1; merge udda1993_0 (in = in_udda1993 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1993=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1993; merge udda1993_1 (in = in_udda1993) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1993=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1993;
proc sort;
by pnr;
run;

data udda1994_0; set f.udda1994;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1994_1; merge udda1994_0 (in = in_udda1994 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1994=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1994; merge udda1994_1 (in = in_udda1994) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1994=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1994;
proc sort;
by pnr;
run;

data udda1995_0; set f.udda1995;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1995_1; merge udda1995_0 (in = in_udda1995 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1995=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1995; merge udda1995_1 (in = in_udda1995) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1995=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1995;
proc sort;
by pnr;
run;

data udda1996_0; set f.udda1996;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1996_1; merge udda1996_0 (in = in_udda1996 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1996=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1996; merge udda1996_1 (in = in_udda1996) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1996=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1996;
proc sort;
by pnr;
run;

data udda1997_0; set f.udda1997;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1997_1; merge udda1997_0 (in = in_udda1997 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1997=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1997; merge udda1997_1 (in = in_udda1997) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1997=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e;		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1997;
proc sort;
by pnr;
run;

data udda1998_0; set f.udda1998;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1998_1; merge udda1998_0 (in = in_udda1998 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1998=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1998; merge udda1998_1 (in = in_udda1998) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1998=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e;	/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1998;
proc sort;
by pnr;
run;

data udda1999_0; set f.udda1999;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda1999_1; merge udda1999_0 (in = in_udda1999 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda1999=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda1999; merge udda1999_1 (in = in_udda1999) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda1999=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 1999;
proc sort;
by pnr;
run;

data udda2000_0; set f.udda2000;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda2000_1; merge udda2000_0 (in = in_udda2000 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda2000=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2000; merge udda2000_1 (in = in_udda2000) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2000=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e;		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 2000;
proc sort;
by pnr;
run;

data udda2001_0; set f.udda2001;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda2001_1; merge udda2001_0 (in = in_udda2001 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda2001=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2001; merge udda2001_1 (in = in_udda2001) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2001=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 2001;
proc sort;
by pnr;
run;

data udda2002_0; set f.udda2002;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda2002_1; merge udda2002_0 (in = in_udda2002 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda2002=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2002; merge udda2002_1 (in = in_udda2002) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2002=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 2002;
proc sort;
by pnr;
run;

data udda2003_0; set f.udda2003;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda2003_1; merge udda2003_0 (in = in_udda2003 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda2003=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2003; merge udda2003_1 (in = in_udda2003) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2003=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 2003;
proc sort;
by pnr;
run;

data udda2004_0; set f.udda2004;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda2004_1; merge udda2004_0 (in = in_udda2004 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda2004=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2004; merge udda2004_1 (in = in_udda2004) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2004=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 2004;
proc sort;
by pnr;
run;

data udda2005_0; set f.udda2005;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda2005_1; merge udda2005_0 (in = in_udda2005 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda2005=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2005; merge udda2005_1 (in = in_udda2005) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2005=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e;		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 2005;
proc sort;
by pnr;
run;

data udda2006_0; set f.udda2006;
almudd_num=input(almudd,4.); 					*Character to nummeric;
hfudd_num=input(hfudd,4.);
drop almudd hfudd almafgtp ekafgtp hfafgtp hfkilde almfsp ekfsp;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -	 		Note: no missing afsp1e codes;
data udda2006_1; merge udda2006_0 (in = in_udda2006 rename=(almudd_num=almudd hfudd_num=hfudd)) audd (in = in_audd rename=(audd_num=almudd));
by almudd;
if in_udda2006=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2006; merge udda2006_1 (in = in_udda2006) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2006=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 		/*Danner ekfsp*/
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
d_missing = 0;
aar = 2006;
proc sort;
by pnr;
run;

data udda2007_0; set f.udda2007;				*character to nummeric;
almudd_num=input(almudd,4.);
hfudd_num=input(hfudd,4.);
igudd_num=input(igudd,4.);
drop almudd ekudd igudd hfudd almafgtp ekafgtp hfafgtp hf_kilde;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -			Note: no missing afsp1e codes;
data udda2007_1; merge udda2007_0 (in = in_udda2007  rename=(almudd_num=almudd hfudd_num=hfudd igudd_num=igudd)) audd (in =in_audd  rename=(audd_num=almudd));
by almudd;
if in_udda2007=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2007_2; merge udda2007_1 (in = in_udda2007) audd (in = in_audd rename=(audd_num=hfudd));
by hfudd;
if in_udda2007=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e; 	/*Danner ekfsp*/
drop afsp1e;
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
proc sort;
by igudd;
run;
*FSP1E CODES;
data udd ; set g.uddan_2011_udd (keep = udd fsp1e publ1 h1 m1);
udd_num=input(udd,8.); 						*Character to numeric;
drop udd;
proc sort nodupkey dupout=dupkeys; 				*Currently no duplicates;
by udd_num;
run;
*ADDING FSP1E CODES TO EDUCATION DATA -			Note: no missing fsp1e codes;
data udda2007; merge udda2007_2 (in = in_udda2007) udd (in = in_udd rename=(udd_num=igudd));
by igudd;
if in_udda2007=1;
rename fsp1e = igfsp;
if in_udd=0 and igudd ne . then missing_igfsp=1; else missing_igfsp=0;
d_missing = 0;
aar = 2007;
proc sort;
by pnr;
run;

data udda2008_0; set f.udda2008;				*character to nummeric;
almudd_num=input(almudd,4.);
hfudd_num=input(hfudd,4.);
igudd_num=input(igudd,4.);
drop almudd ekudd igudd hfudd almafgtp ekafgtp hfafgtp hf_kilde;
proc sort;
by almudd_num;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -			Note: no missing afsp1e codes;
data udda2008_1; merge udda2008_0 (in = in_udda2008  rename=(almudd_num=almudd hfudd_num=hfudd igudd_num=igudd)) audd (in =in_audd  rename=(audd_num=almudd));
by almudd;
if in_udda2008=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2008_2; merge udda2008_1 (in = in_udda2008) audd (in = in_audd rename=(audd_num=hfudd)); 
by hfudd;
if in_udda2008=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e;		/*Danner ekfsp*/
drop afsp1e;
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
proc sort;
by igudd;
run;
*ADDING FSP1E CODES TO EDUCATION DATA -			Note: no missing fsp1e codes;
data udda2008; merge udda2008_2 (in = in_udda2008) udd (in = in_udd rename=(udd_num=igudd));
by igudd;
if in_udda2008=1;
rename fsp1e = igfsp;
if in_udd=0 and igudd ne . then missing_igfsp=1; else missing_igfsp=0;
d_missing = 0;
aar = 2008;
proc sort;
by pnr;
run;

data udda2009_0; set k.udda2009;				*character to nummeric;
almudd=almaudd;
hfudd=erhaudd;
igudd=input(udd,4.);
drop almaudd alm_vfra erhaudd erh_vfra hf_vfra hfaudd hf_kilde udd;
proc sort;
by almudd;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -			Note: no missing afsp1e codes;
data udda2009_1; merge udda2009_0 (in = in_udda2009) audd (in =in_audd  rename=(audd_num=almudd));
by almudd;
if in_udda2009=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2009_2; merge udda2009_1 (in = in_udda2009) audd (in = in_audd rename=(audd_num=hfudd)); 
by hfudd;
if in_udda2009=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e;		/*Danner ekfsp*/
drop afsp1e;
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
proc sort;
by igudd;
run;
*ADDING FSP1E CODES TO EDUCATION DATA -			Note: no missing fsp1e codes;
data udda2009; merge udda2009_2 (in = in_udda2009) udd (in = in_udd rename=(udd_num=igudd));
by igudd;
if in_udda2009=1;
rename fsp1e = igfsp;
if in_udd=0 and igudd ne . then missing_igfsp=1; else missing_igfsp=0;
d_missing = 0;
aar = 2009;
proc sort;
by pnr;
run;

data udda2010_0; set l.udda2010;				*character to nummeric;
almudd =almaudd;
hfudd =erhaudd;
igudd =input(udd,4.);
drop almaudd alm_vfra erhaudd erh_vfra hf_vfra hfaudd hf_kilde udd;
proc sort;
by almudd;
run;
*ADDING AFSP1E CODES TO EDUCATION DATA -			Note: no missing afsp1e codes;
data udda2010_1; merge udda2010_0 (in = in_udda2010) audd (in =in_audd  rename=(audd_num=almudd));
by almudd;
if in_udda2010=1;
if in_audd=0 and almudd ne . then missing_almfsp=1; else missing_almfsp=0;
rename afsp1e = almfsp;
proc sort;
by hfudd;
run;
data udda2010_2; merge udda2010_1 (in = in_udda2010) audd (in = in_audd rename=(audd_num=hfudd)); 
by hfudd;
if in_udda2010=1;
if afsp1e ge 35000000 and afsp1e lt 39000000 or afsp1e ge 40000000 then ekfsp = afsp1e;		/*Danner ekfsp*/
drop afsp1e;
if in_audd=0 and hfudd ne . then missing_hffsp=1; else missing_hffsp=0;
proc sort;
by igudd;
run;
*ADDING FSP1E CODES TO EDUCATION DATA -			Note: no missing fsp1e codes;
data udda2010; merge udda2010_2 (in = in_udda2010) udd (in = in_udd rename=(udd_num=igudd));
by igudd;
if in_udda2010=1;
rename fsp1e = igfsp;
if in_udd=0 and igudd ne . then missing_igfsp=1; else missing_igfsp=0;
d_missing = 0;
aar = 2010;
proc sort;
by pnr;
run;

data s.uddannelse; set udda1980-udda2010;
keep pnr almfsp ekfsp igfsp aar d_missing missing_almfsp missing_igfsp missing_hffsp;
label almfsp = 'Almen skoleuddannelse (fsp)';
label ekfsp = 'Højest fuldførte erhvervskompetencegivende udd. (fsp)';
label igfsp = 'Igangværende uddannelse (fsp)'; 
proc sort;
by pnr;
run;

proc datasets;
delete 	 udda1990_0 udda1990_1 
		udda1980_0 udda1980_1 udda1981_0 udda1981_1 udda1982_0 udda1982_1 udda1983_0 udda1983_1 udda1984_0 udda1984_1 
		udda1985_0 udda1985_1 udda1986_0 udda1986_1 udda1987_0 udda1987_1 udda1988_0 udda1988_1 
		udda1989_0 udda1989_1
		udda1991_0 udda1991_1 udda1992_0 udda1992_1 udda1993_0 udda1993_1 udda1994_0 udda1994_1 
		udda1995_0 udda1995_1 udda1996_0 udda1996_1 udda1997_0 udda1997_1 udda1998_0 udda1998_1 
		udda1999_0 udda1999_1 udda2000_0 udda2000_1 udda2001_0 udda2001_1 udda2002_0 udda2002_1 
		udda2003_0 udda2003_1 udda2004_0 udda2004_1 udda2005_0 udda2005_1 udda2006_0 udda2006_1 
		udda2007_0 udda2007_1 udda2007_2 udda2008_0 udda2008_1 udda2008_2 udda2009_1 udda2009_2 udda2010_1 udda2010_2; 
quit;

*Education mom;
data udd1;
set s.uddannelse;
rename pnr = pnrm;
rename aar = fodt_aar;
run;
proc sort;
by pnrm fodt_aar;
run;

data udd2;
set temp1;
proc sort;
by pnrm fodt_aar;
run;

data udd3; 
set udd1;
basic_edu_mom=0;
hsvoc_edu_mom = 0;
college_mom=0;
if almfsp = "" and ekfsp ="" then basic_edu_mom=.;
if almfsp = 90309920 or almfsp = 90309910 or almfsp le 10011010 and ekfsp="" then basic_edu_mom=.;
if almfsp = "" and ekfsp ="" then hsvoc_edu_mom=.;
if almfsp = 90309920 or almfsp = 90309910 or almfsp le 10011010 and ekfsp="" then hsvoc_edu_mom=.;
if almfsp = "" and ekfsp ="" then college_mom=.;
if almfsp = 90309920 or almfsp = 90309910 or almfsp le 10011010 and ekfsp="" then college_mom=.;
if almfsp ge 10020090 and almfsp lt 20000000 and ekfsp = . then basic_edu_mom = 1;
if almfsp ge 20000000 and almfsp lt 30000000 and ekfsp = . then hsvoc_edu_mom = 1; 
if ekfsp lt 40000000 and ekfsp ne . then hsvoc_edu_mom = 1;
if ekfsp ge 40000000 then college_mom = 1;
if ekfsp gt 70000000 then basic_edu_mom =.;
if ekfsp gt 70000000 then hsvoc_edu_mom =.;
if ekfsp gt 70000000 then college_mom =.;
keep pnrm  fodt_aar basic_edu_mom hsvoc_edu_mom college_mom;
run;

proc sort data=udd3;
by pnrm fodt_aar;
run;

data udd4;
merge udd2 (in = a) udd3;
by pnrm fodt_aar;
if a;
run;

*Education dad;
data udd5;
set s.uddannelse;
rename pnr = pnrf;
rename aar = fodt_aar;
proc sort;
by pnrf fodt_aar;
run;

data udd6;
set udd4;
run;
proc sort;
by pnrf fodt_aar;
run;

data udd7; 
set udd5;
basic_edu_dad=0;
hsvoc_edu_dad = 0;
college_dad=0;
if almfsp = "" and ekfsp ="" then basic_edu_dad=.;
if almfsp = 90309920 or almfsp = 90309910 or almfsp le 10011010 and ekfsp="" then basic_edu_dad=.;
if almfsp = "" and ekfsp ="" then hsvoc_edu_dad=.;
if almfsp = 90309920 or almfsp = 90309910 or almfsp le 10011010 and ekfsp="" then hsvoc_edu_dad=.;
if almfsp = "" and ekfsp ="" then college_dad=.;
if almfsp = 90309920 or almfsp = 90309910 or almfsp le 10011010 and ekfsp="" then college_dad=.;
if almfsp ge 10020090 and almfsp lt 20000000 and ekfsp = . then basic_edu_dad = 1;
if almfsp ge 20000000 and almfsp lt 30000000 and ekfsp = . then hsvoc_edu_dad = 1; 
if ekfsp lt 40000000 and ekfsp ne . then hsvoc_edu_dad = 1;
if ekfsp ge 40000000 then college_dad = 1;
if ekfsp gt 70000000 then basic_edu_dad =.;
if ekfsp gt 70000000 then hsvoc_edu_dad =.;
if ekfsp gt 70000000 then college_dad =.;
keep pnrf  fodt_aar basic_edu_dad hsvoc_edu_dad college_dad;
run;

proc sort data=udd7;
by pnrf fodt_aar;
run;

data udd8; merge udd6 (in = a) udd7;
by pnrf fodt_aar;
if a;
run;

Data nicu_v1;
set udd8;
run;/*Uddannelses niveauet på forældre er nu tilføjet*/

*******************************************************************************************
Add parents income the year before birth, income_dad, income_mom total_income
*******************************************************************************************;
*Henter persondata ind først, indenholder info på løn samt alder.;
data persondata1980; set d.persondata1980;
aar = 1980; 
run;
data persondata1981; set d.persondata1981;
aar = 1981; 
run;
data persondata1982; set d.persondata1982;
aar = 1982; 
run;
data persondata1983; set d.persondata1983;
aar = 1983; 
run;
data persondata1984; set d.persondata1984;
aar = 1984; 
run;
data persondata1985; set d.persondata1985;
aar = 1985; 
run;
data persondata1986; set d.persondata1986;
aar = 1986; 
run;
data persondata1987; set d.persondata1987;
aar = 1987; 
run;
data persondata1988; set d.persondata1988;
aar = 1988; 
run;
data persondata1989; set d.persondata1989;
aar = 1989; 
run;
data persondata1990; set d.persondata1990;
aar = 1990; 
run;
data persondata1991; set d.persondata1991;
aar = 1991; 
run;
data persondata1992; set d.persondata1992;
aar = 1992; 
run;
data persondata1993; set d.persondata1993;
aar = 1993; 
run;
data persondata1994; set d.persondata1994;
aar = 1994; 
run;
data persondata1995; set d.persondata1995;
aar = 1995; 
run;
data persondata1996; set d.persondata1996;
aar = 1996; 
run;
data persondata1997; set d.persondata1997;
aar = 1997; 
run;
data persondata1998; set d.persondata1998;
aar = 1998; 
run;
data persondata1999; set d.persondata1999;
aar = 1999; 
run;
data persondata2000; set d.persondata2000;
aar = 2000; 
run;
data persondata2001; set d.persondata2001;
aar = 2001; 
run;
data persondata2002; set d.persondata2002;
aar = 2002; 
run;
data persondata2003; set d.persondata2003;
aar = 2003; 
run;
data persondata2004; set d.persondata2004;
aar = 2004; 
run;
data persondata2005; set d.persondata2005;
aar = 2005; 
run;
data persondata2006; set d.persondata2006;
aar = 2006; 
run;
data persondata2007; set d.persondata2007;
aar = 2007; 
run;
data persondata2008; set d.persondata2008;
aar = 2008; 
run;
data persondata2009; set k.persondata2009;
aar = 2009;
kon2_char = put(kon2, 1.);
bkom_char = put(bkom, 3.);
drop kon2 bkom;
rename kon2_char = kon2 bkom_char = bkom;
run;
data persondata2010; set d.persondata2010;
aar = 2010; 
kon2_char = put(kon2, 1.);
bkom_char = put(bkom, 3.);
drop kon2 bkom;
rename kon2_char = kon2 bkom_char = bkom;
run; 

data s.persondata; set persondata1980-persondata2010;
/* Deflator med basisår = 2000 udfra forbrugerprisindex*/
if aar = 1980 then deflation = 45.6;
if aar = 1981 then deflation = 51.0;
if aar = 1982 then deflation = 56.1;
if aar = 1983 then deflation = 60.0;
if aar = 1984 then deflation = 63.8;
if aar = 1985 then deflation = 66.7;
if aar = 1986 then deflation = 69.2;
if aar = 1987 then deflation = 72.0;
if aar = 1988 then deflation = 75.3;
if aar = 1989 then deflation = 78.9;
if aar = 1990 then deflation = 80.9;
if aar = 1991 then deflation = 82.9;
if aar = 1992 then deflation = 84.6;
if aar = 1993 then deflation = 85.7;
if aar = 1994 then deflation = 87.4;
if aar = 1995 then deflation = 89.2;
if aar = 1996 then deflation = 91.1;
if aar = 1997 then deflation = 93.1;
if aar = 1998 then deflation = 94.8;
if aar = 1999 then deflation = 97.2;
if aar = 2000 then deflation = 100;
if aar = 2001 then deflation = 102.4;
if aar = 2002 then deflation = 104.8;
if aar = 2003 then deflation = 107.0;
if aar = 2004 then deflation = 108.3;
if aar = 2005 then deflation = 110.2;
if aar = 2006 then deflation = 112.3;
if aar = 2007 then deflation = 114.2;
if aar = 2008 then deflation = 118.1;
if aar = 2009 then deflation = 119.7;
if aar = 2010 then deflation = 122.4;
earnings = (lonind/(deflation/100)); 
proc sort;
by pnr;
run;

proc datasets;
delete persondata1980-persondata2010;
quit;

data temp1; 
set s.persondata;
if earnings le 0 then earnings = .;
keep pnr aar earnings fodtdato;
run;

*MOR;
data temp2;
set temp1;
rename pnr = pnrm;
rename earnings = income_mom;
rename fodtdato = fodtdato_mor;
fodt_aar = aar+1; /* Indkomsten i barnets fødselsår, "sæt aar+1 hvis året før fødslen skal bruges"*/  
run;

proc sort data=temp2;
by pnrm fodt_aar;
run;

Proc sort data=nicu_v1;
by pnrm fodt_aar;
run;

data merge_mor;
merge nicu_v1 (in=a) temp2;
by pnrm fodt_aar;
if a;
run;

proc sort data=merge_mor nodupkey;
by pnr;
run;

*FAR;
data temp3;
set temp1;
rename pnr = pnrf;
rename earnings = income_dad;
rename fodtdato = fodtdato_far;
fodt_aar = aar+1;
run;

proc sort data=temp3;
by pnrf fodt_aar;
run;

proc sort data=merge_mor;
by pnrf fodt_aar;
run;

data merge_far;
merge merge_mor (in=a) temp3;
by pnrf fodt_aar;
if a;
run;

proc sort data=merge_far nodupkey;
by pnr;
run;

Data temp4;
set merge_far;
total_income = income_dad + income_mom;
run;

*******************************************************************************************
Add parents age at birth and at the birth of first child - age_dad, age_mom, age_fb_dad and age_fb_dad 
*******************************************************************************************;
Data nicu_v2;
set temp4;
age_mom = year(fodtdato) - year(fodtdato_mor);
age_dad = year(fodtdato) - year(fodtdato_far);
age_fb_mom= birthyear_fb - year(fodtdato_mor);
age_fb_dad= birthyear_fb - year(fodtdato_far);
drop aar fodtdato_far fodtdato_mor;
run;


*******************************************************************************************
Add employment status the year prior to birth, employed_dad and employed_mom
*******************************************************************************************;
*FAR;
Data temp1;
set s.persondata;
keep pnr pstill2 fodt_aar employed_dad;
rename pnr =pnrf;
fodt_aar = aar+1;
If pstill2 in(40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57) or pstill2 in(90,91,92,93,94,95,96,97,98) then employed_dad=0; else employed_dad=1;
run;

Data temp2;
set temp1;
drop pstill2;
run;

proc sort data=temp2;
by pnrf fodt_aar;
run;
proc sort data=nicu_v2;
by pnrf fodt_aar;
run;

data merge1;
merge nicu_v2 (in=a) temp2;
by pnrf fodt_aar;
if a;
run;

*Mor;
Data temp3;
set s.persondata;
keep pnr pstill2 fodt_aar employed_mom;
rename pnr =pnrm;
fodt_aar = aar+1;
If pstill2 in(40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57) or pstill2 in(90,91,92,93,94,95,96,97,98) then employed_mom=0; else employed_mom=1;
run;

Data temp4;
set temp3;
drop pstill2;
run;

proc sort data=temp4;
by pnrm fodt_aar;
run;
proc sort data=merge1;
by pnrm fodt_aar;
run;

data merge2;
merge merge1 (in=a) temp4;
by pnrm fodt_aar;
if a;
run;

*******************************************************************************************
Add parental immigration status - immigrant_mom and immigrant_dad
*******************************************************************************************;
*MOR;
data temp5;
set k.iepe2010;
rename pnr = pnrm;
rename ie_type = ietype opr_land = ieland; *Bruger ietype hvis 1 ikke immigrant hvis 2 eller 3 immigrant;
proc sort data=temp5;
by pnrm;
run;
proc sort data=merge2;
by pnrm;
run;
data indvandrer; 
merge merge2 (in = a) temp5;
by pnrm;
if a;
run;

data indvandrer1; 
set indvandrer;
if ietype=1 then immigrant_mom=0;
if ietype=2 or ietype=3 then immigrant_mom=1;
drop ietype ieland ielandg1 ielandg2;
run;

*FAR;
data temp6;
set k.iepe2010;
rename pnr = pnrf;
rename ie_type = ietype opr_land = ieland;
proc sort data=temp6;
by pnrf;
run;
proc sort data=indvandrer1;
by pnrf;
run;

data indvandrer2;
merge indvandrer1 (in = a) temp6;
by pnrf;
if a;
run;

data indvandrer3; 
set indvandrer2;
if ietype=1 then immigrant_dad=0;
if ietype=2 or ietype=3 then immigrant_dad=1;
drop ietype ieland ielandg1 ielandg2;
run;

Data Nicu_v3;
set indvandrer3;
run;
proc sort data=nicu_v3 nodupkey;
by pnr;
run;

*******************************************************************************************
Add marriage status at birth - Married_birth
*******************************************************************************************;
data cfamdata1980; set h.cfamdata1980 ;
aar = 1980;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1981; set h.cfamdata1981;
aar = 1981;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1982; set h.cfamdata1982;
aar = 1982;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1983; set h.cfamdata1983;
aar = 1983;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1984; set h.cfamdata1984;
aar = 1984;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1985; set h.cfamdata1985;
aar = 1985;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1986; set h.cfamdata1986;
aar = 1986;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1987; set h.cfamdata1987;
aar = 1987;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1988; set h.cfamdata1988;
aar = 1988;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1989; set h.cfamdata1989;
aar = 1989;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1990; set h.cfamdata1990 ;
aar = 1990;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1991; set h.cfamdata1991;
aar = 1991;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1992; set h.cfamdata1992;
aar = 1992;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1993; set h.cfamdata1993;
aar = 1993;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1994; set h.cfamdata1994;
aar = 1994;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1995; set h.cfamdata1995;
aar = 1995;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1996; set h.cfamdata1996;
aar = 1996;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1997; set h.cfamdata1997;
aar = 1997;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1998; set h.cfamdata1998;
aar = 1998;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata1999; set h.cfamdata1999;
aar = 1999;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2000; set h.cfamdata2000;
aar = 2000;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2001; set h.cfamdata2001;
aar = 2001;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2002; set h.cfamdata2002;
aar = 2002;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2003; set h.cfamdata2003;
aar = 2003;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2004; set h.cfamdata2004;
aar = 2004;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2005; set h.cfamdata2005;
aar = 2005;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2006; set h.cfamdata2006;
aar = 2006;
ctype1=ctype*1;
drop ctype;
run;
data cfamdata2007; set h.efamdata2007;
aar = 2007;
familie_type1=familie_type*1;
drop familie_type;
run;
data cfamdata2008; set f.efamdata2008;
aar = 2008;
drop plads;
familie_type1=familie_type*1;
drop familie_type;
run;
data cfamdata2009; set k.efamdata2009;
aar = 2009;
drop plads;
familie_type1=familie_type*1;
drop familie_type;
run;
data cfamdata2010; set k.efamdata2010;
aar = 2010;
drop plads;
familie_type1=familie_type*1;
drop familie_type;
run;

data cfamdata; set cfamdata1980-cfamdata2010;
proc sort;
by pnr aar;
run;

proc datasets;
delete 	cfamdata1980-cfamdata2010;
quit; 
 
data cfamdata_redigeret;
set cfamdata;
if familie_type1=1 then familie_type2=11;
if familie_type1=2 then familie_type2=21;
if familie_type1=3 then familie_type2=31;
if familie_type1=4 then familie_type2=41;
if familie_type1=5 then familie_type2=1;/*koder familiestatus om så det passer til før 2007 notation*/
if ctype1 =. then ctype1 = familie_type2;
rename aar = fodt_aar;
rename pnr = pnrm;
run;

data fam1;
set cfamdata_redigeret;
keep pnrm fodt_aar ctype1;
run;

Data fam2;
set fam1;
If ctype1 = 11 or ctype1 = 12 then married_birth = 1; else married_birth = 0;
If ctype1 = . then married_birth = .;
keep pnrm fodt_aar married_birth;
run;

proc sort data=nicu_v3;
by pnrm fodt_aar;
run;

Data NICU_v4;
merge nicu_v3 (in=a) fam2;
by pnrm fodt_aar;
if a;
run;

*******************************************************************************************
Add variable for the number of years parents were together before birth - cohabiting
*******************************************************************************************;
Data temp1;
set cfamdata_redigeret;
keep pnrm fodt_aar ctype1;
run;

Data temp2;
set temp1;
cohabiting_birth+1;
by pnrm;
if first.pnrm then cohabiting_birth=1;
if ctype1=1 or ctype1=2 or ctype1=53 then cohabiting_birth=0;
keep pnrm cohabiting_birth fodt_aar;
run;

Data nicu_v5;
merge nicu_v4 (in=a) temp2;
by pnrm fodt_aar;
if a;
run;

*******************************************************************************************
Add variable for if parents are either married or cohabiting in the birth year - married_cohab_birth (dummy variable)
*******************************************************************************************;
Data nicu_v5_1;
set nicu_v5;
if married_birth =1 or cohabiting_birth gt 0 then married_cohab_birth =1; else married_cohab_birth =0;
if married_birth =. and cohabiting_birth = . then married_cohab_birth =.;
run;

*******************************************************************************************
Add variable for divorce 5 and 10 years after birth - divorce5y and divorce10y  TEST!!!!
*******************************************************************************************;
data fam1;
set cfamdata_redigeret;
keep pnrm fodt_aar ctype1;
run;

Data fam2;
set fam1;
If ctype1 = 11 or ctype1 = 12 then married = 1; else married = 0;
If ctype1 = . then married = .;
fodt_aar_5 = fodt_aar;
married_overall = married; *gift det pågældende år;
keep pnrm fodt_aar_5 married_overall;
run;
*divorce5y;
Data temp1;
set nicu_v5_1;
fodt_aar_5=fodt_aar+5;
keep pnrm fodt_aar_5 fodt_aar married_birth;
run;

proc sort data=fam2;
by pnrm fodt_aar_5;
run;
proc sort data=temp1;
by pnrm fodt_aar_5;
run;

Data temp2;
merge fam2 temp1 (in=a);
by pnrm fodt_aar_5;
if a;
run;

Data temp3;
set temp2;
if married_overall - married_birth =-1 then divorce5y=1; else divorce5y=0;
if married_overall =. or married_birth=. then divorce5y=.;
run;

*divorce10y; 
Data temp4;
set nicu_v5_1;
fodt_aar_10=fodt_aar+10;
keep pnrm fodt_aar_10 fodt_aar married_birth;
run;
Data temp5;
set fam2;
fodt_aar_10=fodt_aar_5;
keep pnrm fodt_aar_10 married_overall;
run;
proc sort data=temp5;
by pnrm fodt_aar_10;
run;
proc sort data=temp4;
by pnrm fodt_aar_10;
run;

Data temp6;
merge temp5 temp4 (in=a);
by pnrm fodt_aar_10;
if a;
run;

Data temp7;
set temp6;
if married_overall - married_birth =-1 then divorce10y=1; else divorce10y=0;
if married_overall =. or married_birth=. then divorce10y=.;
run;

Data temp8;
merge temp7 temp3;
by pnrm fodt_aar;
keep pnrm fodt_aar divorce10y divorce5y;
run;

Data nicu_v6;
merge nicu_v5_1 (in=a) temp8;
by pnrm fodt_aar;
run;

*******************************************************************************************
Add variable for emplyment status 5 and 10 years after birth - employed_mom5y, employed_mom10y and employed_dad5y, employed_dad10y
*******************************************************************************************;
*Mother;
Data temp1;
set s.persondata;
keep pnr pstill2 aar employed_mom;
rename pnr =pnrm;
If pstill2 in(40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57) or pstill2 in(90,91,92,93,94,95,96,97,98) then employed_mom=0; else employed_mom=1;
run;

Data temp2;
set nicu_v6;
keep pnrm fodt_aar;
run;

proc sort data=temp1;
by pnrm;
run;

data temp3;
merge temp1 temp2;
by pnrm;
run;

Data temp4;
set temp3;
time = aar - fodt_aar;
if time ne .;
run;

*Employed 5 years after birth;
Data temp5;
set temp4;
if time = 5;
employed_mom5y=employed_mom;
keep pnrm employed_mom5y;
run;

*Employed 10 years after birth;
Data temp6;
set temp4;
if time = 10;
employed_mom10y=employed_mom;
keep pnrm employed_mom10y;
run;

Data temp7;
merge temp6 temp5;
by pnrm;
run;

proc sort data=temp7 nodupkey;
by pnrm;
run; 

Data nicu_v7;
merge nicu_v6 (in=a) temp7;
by pnrm;
if a;
run;

*Father;
Data temp1;
set s.persondata;
keep pnr pstill2 aar employed_dad;
rename pnr =pnrf;
If pstill2 in(40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57) or pstill2 in(90,91,92,93,94,95,96,97,98) then employed_dad=0; else employed_dad=1;
run;

Data temp2;
set nicu_v6;
keep pnrf fodt_aar;
run;

proc sort data=temp1;
by pnrf;
run;

proc sort data=temp2;
by pnrf;
run;

data temp3;
merge temp1 temp2;
by pnrf;
run;

Data temp4;
set temp3;
time = aar - fodt_aar;
if time ne .;
run;

*Employed 5 years after birth;
Data temp5;
set temp4;
if time = 5;
employed_dad5y=employed_dad;
keep pnrf employed_dad5y;
run;

*Employed 10 years after birth;
Data temp6;
set temp4;
if time = 10;
employed_dad10y=employed_dad;
keep pnrf employed_dad10y;
run;

Data temp7;
merge temp6 temp5;
by pnrf;
run;

proc sort data=temp7 nodupkey;
by pnrf;
run; 

proc sort data=nicu_v7;
by pnrf;
run;

Data nicu_v7_1;
merge nicu_v7 (in=a) temp7;
by pnrf;
if a;
run;

*******************************************************************************************
Add mortality variables - dead24h, dead28d and dead1y
*******************************************************************************************;
Proc sort data=nicu_v7_1 nodupkey;
by pnr;
run; 

Data Merge1;
Merge nicu_v7_1(in=a) d.Dode2011;
by pnr;
if a;
run; 

/*Brug Merge1 til at finde levetiden for hver pnr*/
Data temp1;
set Merge1;
levetid = doddato - fodtdato; /* Levetid i dage*/
drop DOEDSAARS1 DOEDSAARS2 DOEDSAARS3 DOEDSMAADE HAENDSTED;
run;

Data temp2;
set temp1;
if levetid le 1 then dead24h = 1; else dead24h = 0; 
If levetid = . then dead24h = 0;
if levetid le 28 then dead28d = 1; else dead28d = 0; 
If levetid = . then dead28d = 0;
if levetid le 365 then dead1y = 1; else dead1y = 0;
If levetid = . then dead1y = 0; 
/* Assume if dodsdato is missing then the person is not dead*/ 
run; 


data nicu_v8;
set temp2;
if fodt_aar lt 1960 then dead24h =.; 
if fodt_aar lt 1960 then dead28d =.;
if fodt_aar lt 1960 then dead1y =.;
if levetid=-392 then dead24h=.;
if levetid=-392 then dead28d=.;
if levetid=-392 then dead1y=.;
run;



*******************************************************************************************
Add school outcomes to master sample
*******************************************************************************************;
*Henter skoledata;
data karakter2013;
set d.udfk2013;
run;

proc freq data=karakter2013;
table skoleaar;
run;


*Fokuserer på karakterer i 9. klasse;
data karak9kl;
set karakter2013;
if kltrin="09" then output karak9kl;
run;


*Beholder kun observationer i Dansk eller Matematik;
data karak9kl_a;
set  karak9kl(rename=(opr_karakter=karakter));

if grundskolefag in ("Dansk" , "Matematik") then output karak9kl_a;
run;


proc freq data=karak9kl_a;
table FAGDISCIPLIN*skoleaar;
run;


data karak9kl_b;
set  karak9kl_a;

if FAGDISCIPLIN in ("Mundtlig" "Skriftlig" "Matematisk problemløsning" "Matematiske færdigheder") then output karak9kl_b;
run;


*Sletter mundtlig matematik observationerne;
data karak9kl_b;
set  karak9kl_b;

if FAGDISCIPLIN="Mundtlig" and grundskolefag="Matematik" then delete;
run;

proc freq data=karak9kl_b;
table fagkode;
run;


*deler op på års- og eksamenskarakter;
data karak9kl_c;
set karak9kl_b;

if proeveform="Standpunkt" then aarskarakter=karakter;
if proeveform in ("Prøvefag 2002-2006" "Bundne prøvefag") then eksamenskarakter=karakter;

run;



data karak9kl_d;
set karak9kl_c;

*Dansk - aarskarakter/stdpunktskarakter;

if GRUNDSKOLEFAG="Dansk" and FAGDISCIPLIN="Mundtlig" and proeveform="Standpunkt" then Dansk_m=karakter;
if GRUNDSKOLEFAG="Dansk" and FAGDISCIPLIN="Skriftlig" and proeveform="Standpunkt" then Dansk_s=karakter;

*Matematik aarskarakter/stdpunktskarakter;

if GRUNDSKOLEFAG="Matematik" and FAGDISCIPLIN in ("Skriftlig", "Matematisk problemløsning" "Matematiske færdigheder") and proeveform="Standpunkt" then Mat_s=karakter;


*Dansk afgangsprøve/eksamenskarakter;
if GRUNDSKOLEFAG="Dansk" and FAGDISCIPLIN="Mundtlig" and proeveform in ("Prøvefag 2002-2006" "Bundne prøvefag") then Dansk_m_prove=karakter;
if GRUNDSKOLEFAG="Dansk" and FAGDISCIPLIN="Skriftlig" and proeveform in ("Prøvefag 2002-2006" "Bundne prøvefag") then Dansk_s_prove=karakter;


*Til afgangsprøve;
if GRUNDSKOLEFAG="Matematik" and FAGDISCIPLIN in ("Skriftlig", "Matematisk problemløsning" "Matematiske færdigheder") and proeveform in ("Prøvefag 2002-2006" "Bundne prøvefag") then Mat_s_prove=karakter;

run;


proc print data=karak9kl_d (obs=1000);
var Grundskolefag fagdisciplin proeveform Dansk_m Dansk_s Mat_s Dansk_m_prove Dansk_s_prove Mat_s_prove;
run;


*År hvor 9. klasse er afsluttet;
data karak9kl_e;
set karak9kl_d;
year2=substr(skoleaar,6,4);
year1=year2*1;
drop year2;
run;


*Beregner karakter gns;
*Udvælger elevens karakterer fra den første gang eleven går i 9. klasse;

proc sort data= karak9kl_e;
by pnr year1;
run;

data karak9kl1;
set karak9kl_e;

if lag(year1)^=year1 and lag(pnr)=pnr then d=1;
run;

data karak9kl1;
set karak9kl1;
by pnr;

if first.pnr then d2=d;
else d2+d;
run;

data karak9kl1;
set karak9kl1;
if d2^=. then delete;
run;


*Beregner middelvædi og standardafvigelse for at kunne standardisere. De beregnes for hvert år.;

proc printto print='K:\data\workdata\702727\fbni2727\outcomes.txt';
run;

*Jeg finder gennemsnitskarakteren per år;
proc sort data=karak9kl1;
by year1;
run;

*Samlet karakter;

proc sql;
create table karak1 as
select *,  mean(karakter)label="Mean karakter" as mean_karakter
from karak9kl1
group by year1;
quit;

proc print data=karak1 noobs;
run;

proc sql;
create table karak2 as
select *,  std(karakter)label="Std karakter" as std_karakter
from karak1
group by year1;
quit;

proc print data=karak2 noobs;
run;

*Årskarakter;
proc sql;
create table karak3 as
select *,  mean(aarskarakter)label="Mean aarskarakter" as mean_aarskarakter
from karak2
group by year1;
quit;

proc print data=karak3 noobs;
run;

proc sql;
create table karak4 as
select *,  std(aarskarakter)label="Std aarskarakter" as std_aarskarakter
from karak3
group by year1;
quit;

proc print data=karak4 noobs;
run;



*Prøvekarakter (eksamens karakter);
proc sql;
create table karak5 as
select *,  mean(eksamenskarakter)label="Mean eksamenskarakter" as mean_eksamenskarakter
from karak4
group by year1;
quit;

proc print data=karak5 noobs;
run;

proc sql;
create table karak6 as
select *,  std(eksamenskarakter)label="Std eksamenskarakter" as std_eksamenskarakter
from karak5
group by year1;
quit;

proc print data=karak6 noobs;
run;


*Mundtlig dansk årskarakter;
proc sql;
create table karak7 as
select *,  mean(Dansk_m)label="Mean Dansk_m" as mean_Dansk_m
from karak6
group by year1;
quit;

proc print data=karak7 noobs;
run;

proc sql;
create table karak8 as
select *,  std(Dansk_m)label="Std Dansk_m" as std_Dansk_m
from karak7
group by year1;
quit;

proc print data=karak8 noobs;
run;


*Skriftlig dansk årskarakter;
proc sql;
create table karak9 as
select *,  mean(Dansk_s)label="Mean Dansk_s" as mean_Dansk_s
from karak8
group by year1;
quit;

proc print data=karak9 noobs;
run;

proc sql;
create table karak10 as
select *,  std(Dansk_s)label="Std Dansk_s" as std_Dansk_s
from karak9
group by year1;
quit;

proc print data=karak10 noobs;
run;


*Skriftlig matematik årskarakter;
proc sql;
create table karak11 as
select *,  mean(Mat_s)label="Mean Mat_s" as mean_Mat_s
from karak10
group by year1;
quit;

proc print data=karak11 noobs;
run;

proc sql;
create table karak12 as
select *,  std(Mat_s)label="Std Mat_s" as std_Mat_s
from karak11
group by year1;
quit;

proc print data=karak12 noobs;
run;

*Mundtlig dansk eksamensskarakter;
proc sql;
create table karak13 as
select *,  mean(Dansk_m_prove)label="Mean Dansk_m_prove" as mean_Dansk_m_prove
from karak12
group by year1;
quit;

proc print data=karak13 noobs;
run;

proc sql;
create table karak14 as
select *,  std(Dansk_m_prove)label="Std Dansk_m_prove" as std_Dansk_m_prove
from karak13
group by year1;
quit;

proc print data=karak14 noobs;
run;


*Skriftlig dansk årskarakter;
proc sql;
create table karak15 as
select *,  mean(Dansk_s_prove)label="Mean Dansk_s_prove" as mean_Dansk_s_prove
from karak14
group by year1;
quit;

proc print data=karak15 noobs;
run;

proc sql;
create table karak16 as
select *,  std(Dansk_s_prove)label="Std Dansk_s_prove" as std_Dansk_s_prove
from karak15
group by year1;
quit;

proc print data=karak16 noobs;
run;


*Skriftlig matematik årskarakter;
proc sql;
create table karak17 as
select *,  mean(Mat_s_prove)label="Mean Mat_s_prove" as mean_Mat_s_prove
from karak16
group by year1;
quit;

proc print data=karak17 noobs;
run;

proc sql;
create table karak18 as
select *,  std(Mat_s_prove)label="Std Mat_s_prove" as std_Mat_s_prove
from karak17
group by year1;
quit;

proc print data=karak18 noobs;
run;

*Afslutter printto -- default destination genoprettes;
proc printto;
run;


*Der standardiseres;

data standardisere;
set karak18;
karakter1=(karakter-mean_karakter)/std_karakter;

aarskarakter1=(aarskarakter-mean_aarskarakter)/std_aarskarakter;
eksamenskarakter1=(eksamenskarakter-mean_eksamenskarakter)/std_eksamenskarakter;

Dansk_m1=(Dansk_m-mean_Dansk_m)/std_Dansk_m;
Dansk_s1=(Dansk_s-mean_Dansk_s)/std_Dansk_s;
Mat_s1=(Mat_s-mean_Mat_s)/std_Mat_s;

Dansk_m_prove1=(Dansk_m_prove-mean_Dansk_m_prove)/std_Dansk_m_prove;
Dansk_s_prove1=(Dansk_s_prove-mean_Dansk_s_prove)/std_Dansk_s_prove;
Mat_s_prove1=(Mat_s_prove-mean_Mat_s_prove)/std_Mat_s_prove;
run;


proc means maxdec=7 data=standardisere;
class year1;
var karakter1;
run;


*Kollapser for at bestemme antal børn;
data standardisere1;
set standardisere(keep=pnr year1 instnr karakter1 aarskarakter1 eksamenskarakter1
Dansk_m1 Dansk_s1 Mat_s1 Dansk_m_prove1 Dansk_s_prove1 Mat_s_prove1 rename=(instnr=i));
instnr=i*1;
run;


*Kollapser per pnr for at kunne bestemme antallet af børn;
proc means data=standardisere1 NWAY NOPRINT;
class pnr;
Output Out=standardisere_pnr mean=;
run;


*Registrerer om et barn har gennemført 9. klasses afgangsprøve (dansk skriftlig og mundtlig) og matematik (skriftligt);
data afgangsprove;
set karak9kl_e(keep= pnr year1 Mat_s_prove Dansk_m_prove Dansk_s_prove);
run;

*tager middelværdi af eksamenskarakterne i afgangsprøven. De halve karakterer skyldes gns i færdighedsregning samt problem regning i mat;
proc means data=afgangsprove NWAY NOPRINT;
class pnr ;
Output Out=afgangsprove mean=;
run;

data afgangsprove;
set  afgangsprove;
afgangsprove=1;
if Mat_s_prove=. or Dansk_m_prove=. or Dansk_s_prove=. then afgangsprove=0;
keep pnr afgangsprove;
run;

* Merger afgangsprøve med standardisere_pnr;

proc sort data = standardisere_pnr;
by pnr;
run;

proc freq data = standardisere_pnr;
tables karakter1 aarskarakter1 eksamenskarakter1;
run;

proc sort data = afgangsprove;
by pnr;
run;

proc freq data = afgangsprove;
tables afgangsprove;
run;

data outcomes;
merge standardisere_pnr (in=k) afgangsprove;
by pnr;
if k;

*labels;
label karakter1="Overall mark, avr of exam and annual, standardiseret";
label aarskarakter1="Avr yearly mark, 9th grade, standardiseret";
label eksamenskarakter1="Avr exam mark, 9th gradem, standardiseret";
label afgangsprove="Completed 9th grade (0/1)";
label Mat_s_prove1="Written Math Exam, standardiseret";
label Dansk_s_prove1="Written Danish Exam, standardiseret";
label Dansk_m_prove1="Oral Danish Exam, standardiseret";
run;

data outcomes1;
set outcomes;
drop i aarskarakter1 eksamenskarakter1 Dansk_m1 Dansk_s1 Mat_s1 /*Dansk_s_prove1*/ _TYPE_ _FREQ_;
rename year1=year_completed;
run;

*Kan nu merge dette på min NICU sample;
proc sort data=NICU_v8;
by pnr;
run;
proc sort data=outcomes1;
by pnr;
run;

Data Nicu_v9;
merge nicu_v8 (in=a) outcomes1;
by pnr;
if a;
drop year_completed instnr afgangsprove doddato levetid;
run; 

*******************************************************************************************
Add number of hospitals vistit the first year 
*******************************************************************************************;
*Henter data fra K adhd mappen, hvor alle_diag indenholder indlæggelser diagnoser for hvert individ; 
Data temp1;
set c.alle_diag;
keep pnr dato_diag diag year_diag;
run;
Proc sort data=temp1;
by pnr;
run;

Data temp2;
set NICU_v9;
keep pnr fodtdato;
run;
proc sort;
by pnr;
run;

data merge1;
merge temp1 temp2 (in=a);
by pnr;
if a;
run;
proc sort;
by fodtdato;
run;

Data temp3;
set merge1;
if dato_diag ne .;
if dato_diag - fodtdato le 365 and dato_diag - fodtdato ge 1;
tid =dato_diag - fodtdato;
run;

proc sort data=temp3 nodupkey; /* Fjerner dubbler, hvor pnr og dato_diag er den samme, altså de får flere diagnoser på samme dag.*/
by pnr tid;
run;

proc sort data=temp3;
by pnr;
run;

Data temp4;
set temp3;
Hospital_contact + 1;
by pnr;
if first.pnr then Hospital_contact = 1;
run; 

Data temp5;
set temp4;
by pnr;
if last.pnr then last_pnr =1;
run;

Data temp6;
set temp5;
if last_pnr=1;
run; 

Data temp8;
set merge1;
if dato_diag ne .;
if dato_diag - fodtdato le 365 and dato_diag - fodtdato ge 0;
tid =dato_diag - fodtdato;
run;
proc sort data=temp8 nodupkey; /* Fjerner dubbler, hvor pnr og tid er den samme, altså de får flere diagnoser på samme dag.*/
by pnr tid;
run; 

Data merge3;
merge temp8 temp6;
by pnr;
run;
Data temp9;
set merge3;
if tid=0 then Hospital_contact = 0;
run;
proc sort data=temp9 nodupkey; 
by pnr;
run;

Data temp10; /*Drop alle variabler i temp9 undtagen pnr og Hospital_contact*/
set temp9;
keep pnr Hospital_contact;
run; 

Proc sort data=NICU_v9;
by pnr;
run;

Data merge2;
merge NICU_v9 temp10;
by pnr;
run;

Data NICU_v10;
set merge2;
drop FTDB_LBNR;
run; 

*******************************************************************************************
Add mother numbers of hospitals vistit half a year prior to birth
*******************************************************************************************;
Data temp1;
set c.alle_diag;
keep pnr dato_diag;
run;

Data temp2;
set temp1;
rename pnr=pnrm;
run;

Proc sort data=temp2;
by pnrm;
run;

Data temp3;
set NICU_v10;
keep pnrm fodtdato;
run;

proc sort;
by pnrm;
run;

data merge1;
merge temp2 temp3(in=a);
by pnrm;
if a;
run;
proc sort;
by fodtdato;
run;

Data temp4;
set merge1;
if dato_diag ne .;
if dato_diag - fodtdato ge -182 and dato_diag - fodtdato lt 0; /*Indlæggelser et halvt år før fødsel*/
tid = dato_diag - fodtdato;
run;

proc sort data=temp4 nodupkey; /* Fjerner dubbler, hvor pnrm og dato_diag er den samme, altså de får flere diagnoser på samme dag.*/
by pnrm dato_diag;
run; 

Data temp5;
set temp4;
Hospital_contact_mom + 1;
by pnrm;
if first.pnrm then Hospital_contact_mom = 1;
run; 

Data temp6;
set temp5;
by pnrm;
if last.pnrm then last_pnrm =1;
run;

Data temp7;
set temp6;
if last_pnrm=1;
run; 

Data temp8;
set temp7;
keep pnrm Hospital_contact_mom;
run;

Proc sort data=nicu_v10;
by pnrm;
run;

Data temp9;
merge nicu_v10(in=a) temp8;
by pnrm;
if a;
run;

Data nicu_v11;
set temp9;
label pnrm = "Personnummer, mor";
run;

*******************************************************************************************
Add smooking during pregnancy - smoking
*******************************************************************************************;
data Nicu_v12;
set nicu_v11;
if fodt_aar ge 1980 and fodt_aar le 1996 then smoking_c = b_ryger;
c_ryger_n = c_ryger + 0;
if c_ryger_n = 0 then c_ryger_d = 0;
if c_ryger_n = 99 then c_ryger_d = .;
if c_ryger_n in(10,11,20,21,22,23,29) then c_ryger_d = 1;
if fodt_aar gt 1996 and fodt_aar le 2010 then smoking_c = c_ryger_d;
smoking=smoking_c*1;
drop c_ryger b_ryger c_ryger_d c_ryger_n smoking_c;
run;

*******************************************************************************************
Add gender variable
*******************************************************************************************;
Data nicu_v13;
set nicu_v12;
if kqn=1 then male=1; else male=0;
if kqn=9 or kqn=0 or kqn=. then male=.;
drop kqn;
run;

*******************************************************************************************
Add daycare variables
*******************************************************************************************;
*Laver char. om til numerisk variabler saa macro kan anvendes;
data b.Tot_dagi2007;
set b.Tot_dagi2007;
ydelsen=ydelse*1;
drop ydelse;
run;
data b.Tot_dagi2007;
set b.Tot_dagi2007;
ydelse=ydelsen;
drop ydelsen;
run;
data b.Tot_dagi2008;
set b.Tot_dagi2008;
ydelsen=ydelse*1;
drop ydelse;
run;
data b.Tot_dagi2008;
set b.Tot_dagi2008;
ydelse=ydelsen;
drop ydelsen;
run;
data b.Tot_dagi2009;
set b.Tot_dagi2009;
ydelsen=ydelse*1;
drop ydelse;
run;
data b.Tot_dagi2009;
set b.Tot_dagi2009;
ydelse=ydelsen;
drop ydelsen;
run;
data b.Tot_dagi2010;
set b.Tot_dagi2010;
ydelsen=ydelse*1;
drop ydelse;
run;
data b.Tot_dagi2010;
set b.Tot_dagi2010;
ydelse=ydelsen;
drop ydelsen;
run;
data b.Tot_dagi2011;
set b.Tot_dagi2011;
ydelsen=ydelse*1;
drop ydelse;
run;
data b.Tot_dagi2011;
set b.Tot_dagi2011;
ydelse=ydelsen;
drop ydelsen;
run;


%macro dagi(start,slut);
%do i=&start %to &slut;

data dagi&i;
set b.tot_dagi&i(keep = pnr ydelse);
year=&i;
run;

%end;
%mend;
%dagi(1995,2011);

data dagi;
set dagi1995-dagi2011;
if ydelse ne .;
if pnr ne .;
proc sort;
by pnr;
run;
proc datasets;
delete 	dagi1995-dagi2011;
quit; 

Data temp1;
set nicu_v13;
keep pnr birthyear;
proc sort;
by pnr;
run;

data merge1;
merge dagi temp1;
by pnr;
care_age=year-birthyear;
if care_age ne .;
if care_age ge 0;
run;

Data temp2; *Dummy for privat vs centralized care at age 5, note only 162 have privat care;
set merge1;
if care_age=5;
if ydelse in(200 201) then priv_care_age5=1; else priv_care_age5=0;
if ydelse in(151, 152, 153, 154, 215, 224, 225, 226, 264) then cen_care_age5=1; else cen_care_age5=0;
if ydelse in(305 999) then priv_care_age5=.;
if ydelse in(305 999) then cen_care_age5=.;
keep pnr priv_care_age5 cen_care_age5;
run;

Data temp3; *Dummy for privat vs centralized vs no care care at age 2;
set merge1;
if care_age=2;
if ydelse in(200 201) then private_age2=1; else private_age2=0;
if ydelse in(151, 152, 153, 154, 215, 224, 225, 226, 264) then centralized_age2=1; else centralized_age2=0;
if ydelse in(305 999) then Notreg_age2=1; else Notreg_age2=0;
keep pnr Notreg_age2 centralized_age2 private_age2;
run;

Data temp4;
set merge1;
age_first_care =care_age;
keep age_first_care pnr;
proc sort nodupkey;
by pnr;
run;

Data temp5;
merge temp2 temp3 temp4;
by pnr;
run;

proc sort data=nicu_v13;
by pnr;
run;

*******************************************************************************************
Add hopspital code for birth place
*******************************************************************************************;
Data temp1;
set NICU_v13;
year_diag = year(FODTDATO);
month_diag = month(fodtdato);
run;

Proc sort data=temp1;
by pnrm year_diag month_diag;
run; 
Proc sort data=s.fodsler;
by pnrm year_diag month_diag;
run; 

Data NICU_merge;
merge temp1 (in=a) s.fodsler;
by pnrm year_diag month_diag;
if a;
run; 

proc sort data=NICU_merge nodupkey;
by pnr;
run; 

Data NICU_v14;
set NICU_merge;
rename sgh = birthplace;
drop year_diag month_diag day_diag dato_diag fodsel_icd8 fodsel_icd10;
if pnrm ne ''; 
run; 

Data s.Nicu_v15;
merge nicu_v14 (in=a) temp5;
by pnr;
if a;
proc sort nodupkey;
by pnr;
run;


*******************************************************************************************
Find bobæl ved fødsel - Opdel på amter og regioner
*******************************************************************************************;
data temp1;
set s.persondata;
keep pnr bkom aar birthyear;
rename pnr = pnrm;
birthyear = aar;
proc sort;
by pnrm birthyear;
run;

data temp2;
set s.Nicu_v15;
keep pnrm pnr birthyear;
proc sort;
by pnrm birthyear;
run;

Data temp3;
merge temp2 temp1;
by pnrm birthyear;
run;

Data temp4;
set temp3;
n_kommune = input(bkom,3.);
proc sort;
by pnrm decending birthyear;
run;

Data temp5;
set temp4;
by pnrm;
retain _n_kommune;
if not missing(n_kommune) then _n_kommune = n_kommune; else n_kommune = _n_kommune;
run;

Data temp6;
set temp5;
keep pnr n_kommune;
if pnr ne '';  
proc sort;
by pnr;
run;

proc sort data= s.Nicu_v15;
by pnr;
run;

Data merge1;
merge s.Nicu_v15 (in=a) temp6;
by pnr;
if a;
run;

*Kommune er tilfojet, nu findes amt og region. Fra sanni info;
Data temp7;
set merge1;
if n_kommune = 101 then amt=13;
if n_kommune = 147 then amt=14;
if 150 <= n_kommune <= 199 then amt=15;
if 200 <= n_kommune <= 249 then amt=20;
if 250 <= n_kommune <= 299 then amt=25;
if 300 <= n_kommune <= 349 then amt=30;
if 350 <= n_kommune <= 399 then amt=35;
if 400 <= n_kommune <= 419 then amt=40;
if 420 <= n_kommune <= 499 then amt=42;
if 500 <= n_kommune <= 549 then amt=50;
if 550 <= n_kommune <= 599 then amt=55;
if 600 <= n_kommune <= 649 then amt=60;
if 650 <= n_kommune <= 699 then amt=65;
if 700 <= n_kommune <= 759 then amt=70;
if 760 <= n_kommune <= 799 then amt=76;
if 800 <= n_kommune <= 899 then amt=80;
run;

*Benytter amt til at opdele paa regioner. OBS ikke helt korrekt da viborg amt er delt mellem midtjylland og nordjylland samt vejle er delt mellem syddanmark og midtjylland;
Data temp8;
set temp7;
if amt=13 or amt=14 or amt=15 or amt=20 or amt=40 then region=4; *Hovedstaden;
if amt=25 or amt=30 or amt=35 then region=5; *sjaelland;
if amt=42 or amt=50 or amt=55 or amt=60 then region=3; *Syddanmark;
if amt=65 or amt=70 or amt=76 then region=2; *Midtjylland;
if amt=80 then region=1; *Nordjylland;
run;

Data s.nicu_master_060814;
set temp8;
rename n_kommune = birth_municip;
rename amt = birth_county;
rename region = birth_region;
rename birthplace = birth_hospital;
drop fodt_aar;
run;

Proc freq data=s.nicu_master_060814;
table apgar hospital_contact hospital_contact_mom;
run;
